package com.xiaoma.myaudience.biz.adapter;

import java.util.List;
import java.util.Map;

import android.R.integer;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

import com.xiaoma.myaudience.R;
import com.xiaoma.myaudience.R.drawable;
import com.xiaoma.myaudience.biz.model.RankingListModel;
import com.xiaoma.myaudience.biz.model.ReviewListModel;
import com.xiaoma.myaudience.biz.view.LoadingImageView;
import com.xiaoma.myaudience.util.PicUtils;
import com.xiaoma.myaudience.util.model.ModelUtils;

/**
 * 评论页面
 * @author jpren
 *
 */
public class ReviewListAdapter extends BaseAdapter{
	private static final long MIN = 60 * 100000;

	private Context mContext;
	private List<Map<String, Object>> mList;	
	private LayoutInflater mInflater;
	private String mSizeStr;
	private int mType;
	
	public ReviewListAdapter(Context context, List<Map<String, Object>> list, int type) {
		mContext = context;
		mType = type;
		mList = list;
		mInflater = LayoutInflater.from(context);
		mSizeStr = PicUtils.composeSize((int) context.getResources().getDimension(
                R.dimen.list_item_img_width), (int) context.getResources().getDimension(
                R.dimen.list_item_img_height));
	}
	
	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.review_list_item, null);
			ViewHolder holder = new ViewHolder();
			holder.header = (LoadingImageView) convertView.findViewById(R.id.img);
			holder.userName = (TextView) convertView.findViewById(R.id.user_name);
			holder.date = (TextView) convertView.findViewById(R.id.date);
			holder.location = (TextView) convertView.findViewById(R.id.location);
			holder.imgContent = (LoadingImageView) convertView.findViewById(R.id.img_content);
			if (mType == 2) {
				holder.imgContent.setVisibility(View.GONE);
			}
			holder.msg = (TextView) convertView.findViewById(R.id.msg);
			holder.score = (TextView) convertView.findViewById(R.id.score);
			holder.ratingBar = (RatingBar) convertView.findViewById(R.id.rating_bar);
			
            convertView.setTag(holder);
		}
		ViewHolder holder = (ViewHolder) convertView.getTag();
		Map<String, Object> item = (Map<String, Object>) getItem(position);
		holder.header.setHeadLoadingDrawable(ModelUtils.getStringValue(item, ReviewListModel.PARAM_AVATAR), mSizeStr);
		holder.imgContent.setLoadingDrawable(ModelUtils.getStringValue(item, ReviewListModel.PARAM_PATH));
		holder.userName.setText(ModelUtils.getStringValue(item, RankingListModel.PARAM_USERNAME));
		String sex = ModelUtils.getStringValue(item, ReviewListModel.PARAM_SEX);
		Drawable sexImg = mContext.getResources().getDrawable( "1".equals(sex) ? drawable.review_mail : drawable.review_famil);
		sexImg.setBounds(0, 0, sexImg.getMinimumWidth(), sexImg.getMinimumHeight());
		holder.userName.setCompoundDrawables(null, null, sexImg, null);
		String date = ModelUtils.getStringValue(item, ReviewListModel.PARAM_DATETIME);
		holder.date.setText(date);
		String location = ModelUtils.getStringValue(item, RankingListModel.PARAM_LOCATION);
		holder.location.setText(location);
		if (TextUtils.isEmpty(location)) {
			holder.location.setVisibility(View.GONE);
		} else {
			holder.location.setVisibility(View.VISIBLE);
		}
		holder.msg.setText(Html.fromHtml(ModelUtils.getStringValue(item, ReviewListModel.PARAM_CONTENT)));
		String score = ModelUtils.getStringValue(item, RankingListModel.PARAM_STARLEVEL);
		holder.score.setText(score);
		holder.ratingBar.setRating(TextUtils.isEmpty(score) ? 0.0f : Float.valueOf(score) / 2);
		
		return convertView;
	}

	private class ViewHolder {
        LoadingImageView header;
        TextView userName;
        TextView location;
        TextView date;
        LoadingImageView imgContent;
        TextView score;
        TextView msg;
        RatingBar ratingBar;
    }
}